import json
import matplotlib.pyplot as plt

'''
根据日志生成测试数据
'''

# 提出log中的json数据
def read_log():
    json_list = []
    pre_data = ''
    with open('data/2024-11-21-agv.log', 'r', encoding='utf-8') as file:
        for line in file:
            if '接收到Redis数据' not in line:
                continue
            line = line[41:]
            if line == pre_data:
                print("跳过此数据")
                continue
            pre_data = line
            json_object = json.loads(line)
            json_list.append(json_object)
    with open('data/2024-12-02-agv.json', 'w', encoding='utf-8') as file:
        json.dump(json_list, file, indent=4, ensure_ascii=False)

# 筛选数据
def filter_data():
    filter_data = []
    with open('data/2024-10-14.json', 'r') as file:
        all_data = json.load(file)
    for json_data in all_data:
        temp = json_data
        json_data = json_data['S7Data']['groundEquipment']
        motorForward12001 = json_data['motorForward12001']
        reverse12001 = json_data['reverse12001']
        probeGoods12001 = json_data['probeGoods12001']
        moveForward14001 = json_data['moveForward14001']
        moveReverse14001 = json_data['moveReverse14001']
        chainForward14001 = json_data['chainForward14001']
        chainReverse14001 = json_data['chainReverse14001']
        probeCargo14001 = json_data['probeCargo14001']
        bool_vars = [motorForward12001, reverse12001, probeGoods12001, moveForward14001, moveReverse14001, chainForward14001, chainReverse14001, probeCargo14001]
        if any(bool_vars):
            filter_data.append(temp)

    with open('data/2024-10-14-filter.json', 'w') as file:
        json.dump(filter_data, file, indent=4, ensure_ascii=False)


# 统计数据
def watch_data():
    s7_data = {
        "stackerOneRealTime": {
            "moveSpeed": [],
            "liftSpeed": [],
            "forkSpeed": [],
            "currentWalkingPosition": [],
            "liftCurrentPosition": [],
            "forkCurrentPosition": [],
            "taskNumber": []
        },
        "stackerTwoRealTime": {
            "moveSpeed": [],
            "liftSpeed": [],
            "forkSpeed": [],
            "currentWalkingPosition": [],
            "liftCurrentPosition": [],
            "forkCurrentPosition": [],
            "taskNumber": []
        }
    }
    with open('data/2024-10-14.json', 'r') as file:
        data = json.load(file)
        for item in data:
            item = item['S7Data']
            s7_data['stackerOneRealTime']['moveSpeed'].append(item['stackerOneRealTime']['moveSpeed'])
            s7_data['stackerOneRealTime']['liftSpeed'].append(item['stackerOneRealTime']['liftSpeed'])
            s7_data['stackerOneRealTime']['forkSpeed'].append(item['stackerOneRealTime']['forkSpeed'])
            s7_data['stackerOneRealTime']['currentWalkingPosition'].append(item['stackerOneRealTime']['currentWalkingPosition'])
            s7_data['stackerOneRealTime']['liftCurrentPosition'].append(item['stackerOneRealTime']['liftCurrentPosition'])
            s7_data['stackerOneRealTime']['forkCurrentPosition'].append(item['stackerOneRealTime']['forkCurrentPosition'])
            s7_data['stackerOneRealTime']['taskNumber'].append(item['stackerOneRealTime']['taskNumber'])
            s7_data['stackerTwoRealTime']['moveSpeed'].append(item['stackerOneRealTime']['moveSpeed'])
            s7_data['stackerTwoRealTime']['liftSpeed'].append(item['stackerOneRealTime']['liftSpeed'])
            s7_data['stackerTwoRealTime']['forkSpeed'].append(item['stackerOneRealTime']['forkSpeed'])
            s7_data['stackerTwoRealTime']['currentWalkingPosition'].append(item['stackerOneRealTime']['currentWalkingPosition'])
            s7_data['stackerTwoRealTime']['liftCurrentPosition'].append(item['stackerOneRealTime']['liftCurrentPosition'])
            s7_data['stackerTwoRealTime']['forkCurrentPosition'].append(item['stackerOneRealTime']['forkCurrentPosition'])
            s7_data['stackerTwoRealTime']['taskNumber'].append(item['stackerOneRealTime']['taskNumber'])
    with open('data/change.json', 'w') as file:
        json.dump(s7_data, file, indent=4, ensure_ascii=False)

# 根据统计结果查看数据变化
def draw(data_name):
    with open('data/change.json') as file:
        data = json.load(file)
        moveSpeed = data['stackerOneRealTime'][data_name]
    moveSpeed = [item for item in moveSpeed if abs(item) > 1000]
    # 创建图形
    plt.figure(figsize=(10, 6))

    # 绘制位置的折线图
    plt.plot(moveSpeed, label=data_name, color="blue")

    # 添加标题和标签
    plt.title(data_name)
    plt.xlabel("Time (s)")
    plt.ylabel("Values")

    # 显示图例
    plt.legend()

    # 显示图形
    plt.show()

def union_data():
    with open('data/2024-10-14-filter.json', 'r', encoding='utf-8') as file:
        pre_result_list = json.load(file)
    with open('data/2024-12-02-agv.json', 'r', encoding='utf-8') as file:
        agv_data_list = json.load(file)
    n, m = len(pre_result_list), len(agv_data_list)
    index = 0
    l, r = 0, 0
    result_list = []
    while index < n or index < m:
        pre_result = pre_result_list[l]
        agv_data = agv_data_list[r]
        pre_result['RedisData'] = agv_data
        l = (l + 1) % n
        r = (r + 1) % m
        index += 1
        result_list.append(pre_result)
    with open('data/2024-12-02-hadian.json', 'w', encoding='utf-8') as file:
        json.dump(result_list, file, indent=4, ensure_ascii=False)

if __name__ == '__main__':
    read_log()
    #watch_data()
    #filter_data()
    # draw('moveSpeed')
    # draw('liftSpeed')
    # draw('forkSpeed')
    # draw('currentWalkingPosition')
    # draw('liftCurrentPosition')
    # draw('forkCurrentPosition')
    # draw('taskNumber')
    union_data()